package interfaceDAO;

import org.hibernate.Query;
import org.hibernate.Session;
import hibernateUTIL.HibernateUtil;
import player.Player;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Станислав
 * Date: 27.06.14
 * Time: 22:09
 * To change this template use File | Settings | File Templates.
 */
public class DaoPlayer implements IPlayer {


    @Override
    public List<Player> getAllPlayer() {
        Session currentSession = null;
        try {
            currentSession = HibernateUtil.getSessionFactory().openSession();
            currentSession.beginTransaction();

            return (List<Player>) currentSession.createCriteria(Player.class).list();
        } catch (Exception e) {
            currentSession.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            currentSession.getTransaction().commit();
            if (null != currentSession) {
                currentSession.close();
            }
        }
        return null;
    }

    @Override
    public Player getPlayerByName(String name) {
        Session currentSession = null;
        try {
            currentSession = HibernateUtil.getSessionFactory().openSession();
            currentSession.beginTransaction();

            Query query = currentSession.createQuery("from Player where name = :name");
            query.setParameter("name", name);
            List<Player> res = query.list();
            if (!res.isEmpty()) {
                return res.get(0);
            }
        } catch (Exception e) {
            currentSession.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            currentSession.getTransaction().commit();
            if (null != currentSession) {
                currentSession.close();
            }
        }
        return null;
    }

    @Override
    public Player getPlayerByNickname(String nickName) {
        Session currentSession = null;
        try
        {
            currentSession = HibernateUtil.getSessionFactory().openSession();
            currentSession.beginTransaction();

            Query query = currentSession.createQuery("from Player where nickName = :nick");
            query.setParameter("nick", nickName);
            List<Player> ret = query.list();
            if(!ret.isEmpty())
            {
                return ret.get(0);
            }
        }
        catch(Exception e)
        {
            currentSession.getTransaction().rollback();

            e.printStackTrace();
        }
        finally
        {
            currentSession.getTransaction().commit();
            if(null != currentSession)
            {
                currentSession.close();
            }
        }
        return null;    }

    @Override
    public Player getPlayerByScore(int score) {
        Session currentSession = null;
        try {
            currentSession = HibernateUtil.getSessionFactory().openSession();
            currentSession.beginTransaction();

            Query query = currentSession.createQuery("from Player where score =:scor");
            query.setParameter("scor", score);
            List<Player> res = query.list();
            if (!res.isEmpty()) {
                return res.get(0);
            }
        } catch (Exception e) {
            currentSession.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            currentSession.getTransaction().commit();
            if (null != currentSession) {
                currentSession.close();
            }
        }
        return null;
    }

    @Override
    public boolean addPlayer(Player player) {
        boolean b;

        Session currentSession = null;
        try {
            currentSession = HibernateUtil.getSessionFactory().openSession();
            currentSession.beginTransaction();

            currentSession.save(player);

            currentSession.getTransaction().commit();
            b = true;
        } catch (Exception e) {
            currentSession.getTransaction().rollback();

            e.printStackTrace();
            b = false;
        } finally {
            if (null != currentSession) {
                currentSession.close();
            }
        }
        return b;
    }
}
